package com.util.page;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by reeves on 2015/11/28.
 */
public class GetPageModel {
    public static String pageModelStr = null;
    public static String pageShareModelStr = null;
    private static final Logger LOGGER = Logger.getLogger(GetPageModel.class);

    /**
     * 读取正文模板
     *
     * @param pageModelURL
     * @return
     */
    public static String getPageModelStr(String pageModelURL) {

        if (pageModelStr != null) {
            return pageModelStr;
        } else {
            LOGGER.info("模板字符串为空，需要从文件中读取...");
            File file = new File(pageModelURL);

            try {
                FileInputStream in = new FileInputStream(file);
                int size = in.available();
                byte[] buffer = new byte[size];
                in.read(buffer);
                in.close();
                pageModelStr = new String(buffer, "UTF-8");

            } catch (IOException exception) {
                LOGGER.error("读取文章模板出错...", exception);
                return null;
            }
            return pageModelStr;
        }
    }

    /**
     * 读取分享文章模板
     *
     * @param pageShareModelURL
     * @return
     */
    public static String getPageShareModelStr(String pageShareModelURL) {

        if (pageShareModelStr != null) {
            return pageShareModelStr;
        } else {
            LOGGER.info("模板字符串为空，需要从文件中读取...");
            File file = new File(pageShareModelURL);

            try {
                FileInputStream in = new FileInputStream(file);
                int size = in.available();
                byte[] buffer = new byte[size];
                in.read(buffer);
                in.close();
                pageShareModelStr = new String(buffer, "UTF-8");

            } catch (IOException exception) {
                LOGGER.error("读取文章模板出错...", exception);
                return null;
            }
            return pageShareModelStr;
        }
    }

}
